<?php
namespace app\admin\model;
use think\Model;

class Column extends Model
{
    // 表名
    protected $name = 'column';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    // 追加属性
    protected $append = [];

    public function module(){
        return $this->belongsTo('Module','moduleid','itemid')->field('id,name,enname,relate,relation');
    }
    //modelname 实体名称  relateid {"1":"一对一"},{"2":"多对一"},{"3":"多对多"},{"4":"一对多"}
    public function getColumnsByRelate($modelname = '',$relateid = 1){
        $where = array();
        if($modelname){
            if($relateid == 4){
                $where['rname'] = $modelname;
                $where['relate'] = 2;
            }else{
                $where['cname'] = $modelname;
            }
        }
        if($relateid){
            if($relateid < 4){
              $where['relate'] = $relateid;
            }
        }
        $list = $this->where($where)->select();
        return $list;
    }

    public function getColumnsByRelation($modelname = ''){
        $where['rname'] = $modelname;
        $list = $this->where($where)->select();
        return $list;
    }

    public function getColumnsByModel($name = ''){
        $condition = array();$list = array();
        if($name){$condition['cname'] = $name;}
        $listobjarr = $this->where($condition)->order('listorder')->select();
        if(!empty($listobjarr)) {
            foreach ($listobjarr as $val) {
                $list[] = $val->toArray();
            }
            if (!empty($list)) {return $list;}
            else{return array();}
        }else{
            return array();
        }
    }
}
?>
